/**
 * Main package of the application.
 */
package fr.umlv.m2.android.quizz;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import fr.umlv.m2.android.quizz.database.User;
import fr.umlv.m2.android.quizz.database.UserDb;
import fr.umlv.m2.android.quizz.multiplayer.MultiplayerActivity;
import fr.umlv.m2.android.quizz.options.OptionsActivity;
import fr.umlv.m2.android.quizz.player.EditPlayerActivity;
import fr.umlv.m2.android.quizz.scores.ScoresActivity;
import fr.umlv.m2.android.quizz.singleplayer.SinglePlayerActivity;

/**
 * This class defines home page activity of the application.
 * 
 * @author Gambouele Bertrand.
 * @author Viravout Alexandre.
 */
public class HomepageActivity extends Activity {
	/**
	 * The player name.
	 */
	private TextView playerName;
	/**
	 * The edit player button.
	 */
	private Button editPlayer;
	/**
	 * Single player button.
	 */
	private Button singlePlayer;
	/**
	 * Multiplayer button.
	 */
	private Button multiplayer;
	/**
	 * Scores button.
	 */
	private Button scores;
	/**
	 * Options button.
	 */
	private Button options;
	/**
	 * Help button.
	 */
	private Button help;
	/**
	 * About button.
	 */
	private Button about;
	
	/**
	 * The current user.
	 */
	private User user;
	/**
	 * The intent.
	 */
	private Intent intent;
	/**
	 * The on click handler.
	 */
	private OnClickListener clickHandler;

	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.homepage);
		
		this.playerName = (TextView)findViewById(R.id.playerName);
		this.editPlayer = (Button)findViewById(R.id.homepageEditPlayerButton);
		this.singlePlayer = (Button) findViewById(R.id.singlePlayerButton);
		this.multiplayer = (Button) findViewById(R.id.multiplayerButton);
		this.scores = (Button) findViewById(R.id.scoresButton);
		this.options = (Button) findViewById(R.id.optionsButton);
		this.help = (Button) findViewById(R.id.helpButton);
		this.about = (Button) findViewById(R.id.aboutButton);
		
		UserDb userDb = new UserDb(this);
        userDb.open();
    	
    	this.intent = this.getIntent();
    	this.user = userDb.getUserWithId(this.intent.getExtras().getLong("userId"));
		
		User userFromBdd = userDb.getUserWithId(this.user.getId());
		if(userFromBdd != null) {
			this.playerName.setText(userFromBdd.getName());
		}

		userDb.close();
		
		this.clickHandler = new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.homepageEditPlayerButton:
					intent = new Intent(HomepageActivity.this, EditPlayerActivity.class);
					intent.putExtra("userId", user.getId());
					break;
				case R.id.singlePlayerButton :
					intent = new Intent(HomepageActivity.this, SinglePlayerActivity.class);
					intent.putExtra("userId", user.getId());
					break;
					
				case R.id.multiplayerButton :
					intent = new Intent(HomepageActivity.this, MultiplayerActivity.class);
					intent.putExtra("userId", user.getId());
					break;

				case R.id.scoresButton :
					intent = new Intent(HomepageActivity.this, ScoresActivity.class);
					intent.putExtra("userId", user.getId());
					break;

				case R.id.optionsButton :
					intent = new Intent(HomepageActivity.this, OptionsActivity.class);
					break;

				case R.id.helpButton :
					intent = new Intent(HomepageActivity.this, HelpActivity.class);
					break;

				case R.id.aboutButton :
					intent = new Intent(HomepageActivity.this, AboutActivity.class);
					break;
				default:
					return;
				}

				startActivity(intent);
			}
		};

		this.editPlayer.setOnClickListener(this.clickHandler);
		this.singlePlayer.setOnClickListener(this.clickHandler);
		this.multiplayer.setOnClickListener(this.clickHandler);
		this.scores.setOnClickListener(this.clickHandler);
		this.options.setOnClickListener(this.clickHandler);
		this.help.setOnClickListener(this.clickHandler);
		this.about.setOnClickListener(this.clickHandler);
	}
}